import $axios from "@/network";

/**
 * {新闻公共参数}
 *
 * @author: 宋康
 * @date: 2022/8/25 11:27
 */
export const newsPublic = {
  data() {
    return {
      editor: null,
      html: '',
      toolbarConfig: {
        excludeKeys: [
          'fullScreen',
          'emotion'
        ],
      },
      editorConfig: {
        placeholder: '请输入内容...',
        MENU_CONF: {
          uploadImage: {
            base64LimitSize: 100 * 1024 * 1024 // 100M
          }
        }
      },
      mode: 'default',
      ruleForm: {
        newsTitle: '',
        newsLead: '',
        newsBody: '',
        keywords: [],
        newsTypeIds: [],
      },
      rules: {
        newsTitle: [
          {required: true, message: '请输入新闻标题', trigger: 'blur'},
          {min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur'}
        ],
      },
      inputVisible: false,
      inputValue: '',
      dic: [],
    }
  },
  methods: {
    onCreated(editor) {
      this.editor = Object.seal(editor)
    },
    handleClose(tag) {
      this.ruleForm.keywords.splice(this.ruleForm.keywords.indexOf(tag), 1);
    },
    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs['saveTagInput'].$refs.input.focus();
      });
    },
    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.ruleForm.keywords.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = '';
    },
    /**
     * {返回}
     *
     * @author: 宋康
     * @date: 2022/8/17 10:16
     */
    cancel() {
      this.$router.go(-1)
    },
  },
  beforeDestroy() {
    const editor = this.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时，及时销毁编辑器
  }
}

/**
 * {获取新闻分类}
 *
 * @author: 宋康
 * @date: 2022/8/24 11:39
 */
export const newTypeList = () => {
  return $axios({
    method: 'GET',
    url: '/news/type/query/list'
  })
}

/**
 * {保存新闻分类}
 * @param option
 * @author: 宋康
 * @date: 2022/8/24 16:05
 */
export const saveNewsType = (option) => {
  return $axios({
    method: 'POST',
    url: '/news/type/save',
    data: {
      ...option
    }
  })
}

/**
 * {编辑新闻分类}
 *
 * @author: 宋康
 * @date: 2022/8/24 16:38
 */
export const editNewsType = (option) => {
  return $axios({
    method: 'POST',
    url: '/news/type/edit',
    data: {
      ...option
    }
  })
}

/**
 * {移除指定新闻分类}
 *
 * @author: 宋康
 * @date: 2022/8/24 17:20
 */
export const removeNewsType = (id) => {
  return $axios({
    method: 'POST',
    url: '/news/type/remove',
    params: {
      id
    },
  })
}

/**
 * {获取新闻列表}
 *
 * @author: 宋康
 * @date: 2022/8/24 17:32
 */
export const newList = (current, size, option) => {
  return $axios({
    method: 'POST',
    url: '/news/item/query/page',
    params: {
      current,
      size
    },
    data: {
      ...option
    }
  })
}

/**
 * {获取指定新闻详情}
 *
 * @author: 宋康
 * @date: 2022/8/25 11:29
 */
export const newsByNid = (nid) => {
  return $axios({
    url: '/news/item/query',
    params: {
      nid
    }
  })
}

/**
 * {保存新闻}
 *
 * @author: 宋康
 * @date: 2022/8/25 10:59
 */
export const saveNews = (option) => {
  return $axios({
    method: 'POST',
    url: '/news/item/save',
    data: {
      ...option
    }
  })
}


/**
 * {编辑新闻}
 *
 * @author: 宋康
 * @date: 2022/8/25 14:45
 */
export const editNews = (option) => {
  return $axios({
    method: 'POST',
    url: '/news/item/edit',
    data: {
      ...option
    }
  })
}

/**
 * {移除指定新闻}
 * @param nid
 * @author: 宋康
 * @date: 2022/9/21 18:25
 */
export const removeNews = (nid) => {
  return $axios({
    method: 'POST',
    url: '/news/item/remove',
    params: {
      nid
    }
  })
}

/**
 * {获取新闻分类(免登录)}
 *
 * @author: 宋康
 * @date: 2022/8/27 16:06
 */
export const newsTypeListImmune = () => {
  return $axios({
    method: 'GET',
    url: '/news/type/query/list/immune'
  })
}

/**
 * {获取新闻分页（免登录）}
 * @param current
 * @param size
 * @param option
 * @author: 宋康
 * @date: 2022/8/27 16:38
 */
export const newListImmune = (current, size, option) => {
  return $axios({
    method: 'POST',
    url: '/news/item/query/page/immune',
    params: {
      current,
      size
    },
    data: {
      ...option
    }
  })
}


/**
 * {获取指定新闻详情(免登录)}
 *
 * @author: 宋康
 * @date: 2022/8/30 16:14
 */
export const newsByNidImmune = (nt, nid) => {
  return $axios({
    url: '/news/item/query/immune',
    params: {
      nt,
      nid
    }
  })
}